#include <linux/init.h>
#include <linux/sched.h>
#include <linux/tty.h>
#include <linux/module.h>
#include <linux/time.h>

MODULE_LICENSE("Dual BSD/GPL");


#define outb_p(value,port)                      \
  __asm__ ("outb %%al,%%dx\n"                   \
           "\tjmp 1f\n"                         \
           "1:\tjmp 1f\n"                       \
           "1:"::"a" (value),"d" (port))

#define inb_p(port) ({                                  \
      unsigned char _v;                                 \
      __asm__ volatile ("inb %%dx,%%al\n"               \
                        "\tjmp 1f\n"                    \
                        "1:\tjmp 1f\n"                  \
                        "1:":"=a" (_v):"d" (port));     \
      _v;                                               \
    })


#define CMOS_READ(addr) ({                      \
      outb_p(0x80|addr,0x70);                   \
      inb_p(0x71);                              \
    })
#define BCD_TO_BIN(val) ((val)=((val)&15) + ((val)>>4)*10)


#define NUMLOCK 1
#define CAPSLOCK 2
#define SCROLLLOCK 4

#define SCROLLLED 1
#define NUMLED 2
#define CAPSLED 4

void SendKeyboardLeds(uint8_t locks)
{
	uint8_t leds = 0;
	if (locks & NUMLOCK) leds |= NUMLED;
	if (locks & CAPSLOCK) leds |= CAPSLED;
	if (locks & SCROLLLOCK) leds |= SCROLLLED;
	while (inb_p(0x64) & 2) {}
	outb_p(0xED,0x60);
	while (inb_p(0x64) & 2) {}
	outb_p(leds,0x60);
}

int time_init()
{
  int locks = 0;

  locks ^= NUMLOCK;
  locks ^= CAPSLOCK;
  locks ^= SCROLLLOCK;
  
  SendKeyboardLeds(locks);
  
  return 0;
}

void time_exit()
{
  //outb_p(0xed,0x60); 
  printk("time_exit");
}
    
module_init(time_init);
module_exit(time_exit);
